import { Component, OnInit, ChangeDetectorRef, ElementRef, Inject, forwardRef } from '@angular/core';
import { masterService } from '../../../../lib/master.service';
import { path } from '../../../path.config';
import { AppComponent } from '../../../app.component';
import { NzModalService } from 'ng-zorro-antd';

@Component({
    selector: 'app-payment-method-management',
    templateUrl: './payment-method-management.component.html',
    styleUrls: ['./payment-method-management.component.css'],
    providers: [masterService, forwardRef(() => AppComponent)]
})
export class PaymentMethodManagementComponent implements OnInit {
    // 表格数据路径
    public getPlatformList: any = path.accountRecharge.paymentMethod.getPlatformTypeList;
    // 表格数据路径
    public getList: any = path.accountRecharge.paymentMethod.getPaymentList + "?channel=";
    // 目前启用、禁用按钮
    public isEnabled: any = path.accountRecharge.paymentMethod.editEnabled;
    // 更新
    public updatePaymentList: any = path.accountRecharge.paymentMethod.updatePaymentList;

    public rows: Array<any> = [];

    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        public element: ElementRef,
        public confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) { }

    ngOnInit() {
        this.onChangeTable();
    }

    // 表格数据头部标题
    public columns: Array<any> = [
        { title: '支付方式', name: 'name' },
        { title: '排序', name: 'displayIndex' },
        { title: '默认支付方式', name: 'isDefault' },
        { title: '启用', name: 'isActive' }
    ];

    // 渲染表格
    public onChangeTable(page: any = {}): any {
        this._masterService
            .getData(this.getPlatformList)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data.items;
                    for (let i = 0; i < this.rows.length; i++) {
                        this._masterService.getData(this.getList + this.rows[i].name).subscribe((res) => {
                            if (res.data) {
                                this.rows[i].items = res.data;
                                this.cdRef.detectChanges();
                            }
                        })
                    }
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    // 目前启用、禁用
    private IsEnable(rowName: string, rowId: string, rowEnabled) {
        let postIsEnable = () => {
            let IsEnableObj = {
                channel: rowName,
                id: rowId,
                isActive: 1 - rowEnabled
            }
            this._masterService
                .postData(this.isEnabled, IsEnableObj)
                .subscribe((res) => {
                    if (res.statusCode == 200) {
                        this.onChangeTable();
                    } else if (res.statusCode == 401) {
                        this.app.logOff();
                    } else {
                        this.app.toastError('错误信息： ' + res.errorMessage);
                    }
                })
        }

        this.confirmServ.confirm({
            nzTitle: '是否确认启用或禁用？',
            nzOnOk() {
                postIsEnable();
            }
        })
    }

    // 编辑
    public items: Array<any> = [];
    private EditPayment(rowName: string) {
        let ele = this.element.nativeElement.querySelector("#form" + rowName).querySelectorAll(".grid-item");
        for (let i = 0; i < ele.length; i++) {
            let id = ele[i].querySelector(".hidden").innerText;
            this.items[i] = {};
            this.items[i].id = id;
            this.items[i].name = ele[i].querySelector("#itemName" + id).innerText;
            this.items[i].displayIndex = ele[i].querySelector("#itemDisplayIndex" + id).value;
            this.items[i].isDefault = ele[i].querySelector("#itemIsDefault" + id).checked ? 1 : 0;
            this.items[i].isActive = ele[i].querySelector("#itemIsActive" + id).value;
            this.items[i].flag = ele[i].querySelector(".hide").innerText;
            this.items[i].description = "";
        }
        let EditObj = {
            channel: rowName,
            items: this.items
        }
        this._masterService
            .postData(this.updatePaymentList, EditObj)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.app.toastSuccess('编辑成功！');
                    this.onChangeTable();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息: ' + res.errorMessage)
                }
            })
    }
}
